package nio;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class ReadFolder {

	public static void main(String[] args) {
		File folder = new File("file");
		Path p = FileSystems.getDefault().getPath(folder.getAbsolutePath());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
		try {
			DirectoryStream<Path> list = Files.newDirectoryStream(p);
			System.out.println("Text Files");
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			System.out.println("| File name                 |      Size |  LastModifiedTime   | Execute |  Read | Write |");
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			for(Path a : list){
				if(a.toFile().getName().endsWith(".txt")||a.toFile().getName().endsWith(".docx")){
					String line = "";
					String name = a.toFile().getName();
					if(name.length()>25)
						name = name.substring(0,25);
					line = String.format("| %-25s | %8dB | %19s | %7s | %5s | %5s |",name,a.toFile().length(),df.format(a.toFile().lastModified())
							,a.toFile().canExecute(),a.toFile().canRead(),a.toFile().canWrite());
					System.out.println(line);
				}
			}
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			
			list = Files.newDirectoryStream(p);
			System.out.println("\nMusic Files");
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			System.out.println("| File name                 |      Size |  LastModifiedTime   | Execute |  Read | Write |");
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			for(Path a : list){
				if(a.toFile().getName().endsWith(".mp3")||a.toFile().getName().endsWith(".wav")){
					String line = "";
					String name = a.toFile().getName();
					if(name.length()>25)
						name = name.substring(0,25);
					line = String.format("| %-25s | %8dB | %19s | %7s | %5s | %5s |",name,a.toFile().length(),df.format(a.toFile().lastModified())
							,a.toFile().canExecute(),a.toFile().canRead(),a.toFile().canWrite());
					System.out.println(line);
				}
			}
			System.out.println("+---------------------------+-----------+---------------------+---------+-------+-------+");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
